package com.bys.ots.model;

import lombok.extern.slf4j.Slf4j;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.net.URI;




/**
 * @program: Supply Center
 * @description:
 * @author: Huizhe Yu
 * @create: 2019-07-12 15:24
 */
@Slf4j
@Component
public class MailServiceImpl extends AbstractMailService  {


    public static void sendEmail(String header, String body, String to, String title, String code)throws Exception {

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        ExchangeCredentials credentials = new WebCredentials("tfs.order.transparency@thermofisher.com","Th3rm0@13579");
        service.setCredentials(credentials);
        service.setUrl(new URI("https://mail.office365.com/ews/Exchange.asmx"));
// service.autodiscoverUrl("emailAddress");
        EmailMessage msg= new EmailMessage(service);
//主题
        msg.setSubject(title);

//html
        msg.setBody(MessageBody.getMessageBodyFromText( header + code + body));
        msg.getToRecipients().add(to);
        msg.send();
    }


    public static void main(String[] args) throws Exception
    {
        sendEmail("123",null,"bairui.hua@163.com","Hello world!","");
    }


}
